<?php
/**
* 
*/
class SportModel extends MY_Model
{
	
	public function __construct()
	{
		parent::__construct();
		$this->table = 'nmp_sport';
	}


	//获取列表数据
	public function get_list_data($term='',$page=1,$size=10)
	{
		//查询搜索
		$where = ' where 1 ';
		if (isset($term['cat_id']) && !empty($term['cat_id'])) {

			$where .= " AND s.cat_id =".$term['cat_id'];
		}

		if (isset($term['keyword']) && !empty($term['keyword'])) {

			$where .= " AND s.name LIKE '%".$term['keyword']."%'";
		}

		$query = $this->db->query("SELECT s.id, s.name, s.theme_img, sc.name as cat_name FROM ".$this->table." AS s LEFT JOIN nmp_sport_cat AS sc ON s.cat_id = sc.id".$where." ORDER BY id DESC LIMIT ".($page-1)*$size.",".$size);

		$data = $query->result_array();

		$query = $this->db->query("select COUNT(*)  as num from ".$this->table." as s".$where);
		$row = $query->row_array();

		//总数
		$count = $row['num'];
		//总页数
		$pages = ceil($count/$size);
		
		return array('list'=>$data,'count'=>$count, 'pages'=>$pages);

	}


	//获取单行数据
	public function get_sport_row($sport_id = '')
	{
		$this->db->select("* ");
		$this->db->from($this->table);
		$this->db->where('id', $sport_id);

		$query = $this->db->get();

		return $query->row_array();
	}




	//添加入库
	public function inser_sport_data($arr=array())
	{
		$arr['addtime'] = time();
		if ($this->db->insert($this->table,$arr)) 
		{
			return true;
		}
		else
		{
			return false;
		}

	}

	//更新数据
	public function updata_sport_data($arr =array(),$sport_id ='')
	{
		if ($this->db->update($this->table,$arr,array('id'=>$sport_id))){			
			return true;
		}else{
			return false;
		}

	}



	//添加动作分类
	public function insert_sport_cat($cat_name='',$cat_id='')
	{
		$arr['name'] = $cat_name;

		if (empty($cat_id)) 
		{
			if ($this->db->insert('nmp_sport_cat',$arr)) 
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			if ($this->db->update('nmp_sport_cat',$arr,array('id'=>$cat_id))){			
				return true;
			}else{
				return false;
			}

		}
	}
	

	//删除分类
	public function remove_cat($cat_id)
	{
		return $this->db->delete('nmp_sport_cat', array('id'=>$cat_id));
	}


	//删除
	public function remove_sport($sport_id)
	{
		return $this->db->delete($this->table, array('id'=>$sport_id));
	}



}




?>